package com.test.lucene;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;

public class HelloLucene {

	public static final String INDEX_DIR = "./luceneIndex";

	public static final String DOCS_DIR = "./luceneData";

	public static final String CHARSET_NAME = "UTF-8";

	public static final Analyzer analyzer = new StandardAnalyzer(
			Version.LUCENE_36);

	public static void index() throws CorruptIndexException,
			LockObtainFailedException, IOException {
		IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_36,
				analyzer);
		conf.setOpenMode(OpenMode.CREATE);
		IndexWriter writer = new IndexWriter(FSDirectory.open(new File(
				INDEX_DIR)), conf);

		File[] files = new File(DOCS_DIR).listFiles();
		for (File f : files) {
			indexFile(writer, f);
		}

		writer.close();
	}

	// index uri, last-modified, content of file
	public static void indexFile(IndexWriter writer, File file)
			throws CorruptIndexException, IOException {
		Document doc = new Document();

		Field uriFld = new Field("uri", file.getCanonicalPath(), Store.YES,
				Index.NOT_ANALYZED_NO_NORMS);
		doc.add(uriFld);

		NumericField modFld = new NumericField("lastModified", Store.YES, false);
		modFld.setLongValue(file.lastModified());
		doc.add(modFld);

		Field contentFld = new Field("content", new InputStreamReader(
				new FileInputStream(file), CHARSET_NAME));
		doc.add(contentFld);

		writer.addDocument(doc);
	}

	public static void search(String queryStr) throws CorruptIndexException,
			IOException, ParseException {
		IndexReader reader = IndexReader.open(FSDirectory.open(new File(
				INDEX_DIR)));
		IndexSearcher searcher = new IndexSearcher(reader);
		QueryParser parser = new QueryParser(Version.LUCENE_36, "content",
				analyzer);
		Query query = parser.parse(queryStr);

		TopDocs results = searcher.search(query, null, 100);
		int hits = results.totalHits;
		ScoreDoc[] scoreDocs = results.scoreDocs;
		System.out.println(hits);
		for(ScoreDoc scoreDoc : scoreDocs) {
			Document doc = searcher.doc(scoreDoc.doc);
			String uri = doc.get("uri");
			String lastModified = doc.get("lastModified");
			System.out.println(">>hit : " + uri + "  --  " + lastModified);
		}
		

		searcher.close();
		reader.clone();
	}

	/**
	 * @param args
	 * @throws IOException
	 * @throws LockObtainFailedException
	 * @throws CorruptIndexException
	 * @throws ParseException
	 */
	public static void main(String[] args) throws CorruptIndexException,
			LockObtainFailedException, IOException, ParseException {
		index();
		System.out.println("searching for hello......");
		search("hello");
	}

}
